﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using UtfprLibrary.EntityFramework.ComponentModel;
using System.ComponentModel;
using System.Linq;
using UtfprLibrary.EntityFramework.Helpers;

namespace UtfprLibrary.Windows.Forms.ComponentModel {
    internal partial class UtfprFormEagerLoadEditor : Form {
        #region Properties

        private UtfprBindingNavigatorBase _navigatorBase;

        private object _value;
        public object Value {
            get { return _value; }
        }

        #endregion

        #region Contructors

        public UtfprFormEagerLoadEditor() {
            InitializeComponent();
        }

        public UtfprFormEagerLoadEditor(UtfprBindingNavigatorBase navigatorBase) {
            InitializeComponent();
            this._navigatorBase = navigatorBase;
        }

        #endregion

        #region Event Handlers

        private void UtfprFormEagerLoadEditor_Load(object sender, EventArgs e) {
            fillDataGrid();
        }

        private void _btnOK_Click(object sender, EventArgs e) {
            if (_bsUtfprRelatedEndDescription.Current != null) {
                if (_bsUtfprRelatedEndDescription.List.Count > 0) {
                    _value = _bsUtfprRelatedEndDescription.List.Cast<IUtfprRelatedEndDescription>().ToArray();
                } else {
                    _value = null;
                }
            }
        }

        #endregion

        #region Private Methods

        private void fillDataGrid() {
            if (this._navigatorBase.RelatedEndDescriptions == null || this._navigatorBase.RelatedEndDescriptions[0] == null) {
                _bsUtfprRelatedEndDescription.DataSource = typeof(IUtfprRelatedEndDescription);

                var lstComponents = new List<IUtfprRelatedEndDescription>();

                Type entityType = null;
                var bs = this._navigatorBase.DataSource as BindingSource;

                if (bs != null) {
                    var pdc = (bs as ITypedList).GetItemProperties(null);
                    if (pdc != null || pdc.Count > 0) {
                        //get entity type hosted by bindingsource
                        entityType = pdc[0].ComponentType;
                    }
                }

                foreach (var property in entityType.GetProperties()) {
                    if (ObjectContextHelper.IsRelatedEnd(property)) {
                        lstComponents.Add(new UtfprRelatedEndDescription(false, property.Name));
                    }
                }

                _bsUtfprRelatedEndDescription.DataSource = lstComponents;

                if (lstComponents.Count == 0) {
                    MessageBox.Show("No EntityCollection found.");
                }
            } else {
                _bsUtfprRelatedEndDescription.DataSource = this._navigatorBase.RelatedEndDescriptions.ToList();
            }
        }

        #endregion
    }
}
